package cn.dxj1016.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//练习Thread,实现多线程同步下载图片
public class TestThread2 extends Thread{
    private String url;
    private String name;

    public TestThread2(String url, String name) {
        this.url = url;
        this.name = name;
    }


    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载的文件名为："+name);
    }

    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://p1.music.126.net/SRzkUrJHjbCqdPKHP-UTDA==/109951165168773497.jpg", "1.jpg");
        TestThread2 t2 = new TestThread2("https://p1.music.126.net/SRzkUrJHjbCqdPKHP-UTDA==/109951165168773497.jpg", "2.jpg");
        TestThread2 t3 = new TestThread2("https://p1.music.126.net/SRzkUrJHjbCqdPKHP-UTDA==/109951165168773497.jpg", "3.jpg");
//开启线程
        t1.start();
        t2.start();
        t3.start();

    }
}


//下载器
class WebDownloader {
    //    下载方法
    public void downloader(String url, String name)  {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常");
        }
    }
}
